package com.atguigu.gulimall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {

    // 跨域配置
    private CorsConfiguration corsConfiguration(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedHeader("*");
        //是否允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }

    @Bean
    public CorsWebFilter corsFilter(){
        //使用springcloud gateway时使用的是webflux模式，在reactive包下    不是系统的web模式，如果使用sprin-webmvc则使用CorsFilter
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //注册跨域配置信息
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration());
        //设置跨域配置
        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
